<template>

    <div>
        <el-dialog
                :title="title"
                :visible.sync="dialogVisible"
                width="30%"
                >
            <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="form.username" suffix-icon="el-icon-user"></el-input>
                </el-form-item>
                <el-form-item label="用户密码" prop="password" v-if="upPass">
                    <el-input v-model="form.password" show-password></el-input>
                </el-form-item>
                <el-form-item label="用户头像">
                    <el-upload
                            class="avatar-uploader el-upload1"
                            action="http://localhost:8080/users/upload"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload"
                            with-credentials>
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
            </el-form>
                <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
  </span>
        </el-dialog>

        <el-dialog
                title="重置密码"
                :visible.sync="visible2"
                width="30%"
                >
            <span>确定重置密码?<br/>
                密码为123456
            <input type="text" name="id" v-model="uid" style="display: none"></span>
            <span slot="footer" class="dialog-footer">
    <el-button @click="visible2 = false">取 消</el-button>
    <el-button type="primary" @click="yes()">确 定</el-button>
  </span>
        </el-dialog>

        <el-row>
            <el-col :span="6"><el-button type="success" class="addOne" @click="showWin()">新增</el-button></el-col>
            <el-col :span="12"><el-input v-model="uName" placeholder="搜索用户"></el-input></el-col>
            <el-col :span="6"><el-button icon="el-icon-search" @click="load" circle></el-button></el-col>
        </el-row>
        <el-table
                :data="tableData"
                style="width: 100%">
            <el-table-column
                    prop="username"
                    label="用户名"
            >
            </el-table-column>
            <el-table-column
                    prop="headimg"

                    label="用户头像"
            >
                <template slot-scope="scope">
                    <el-image
                            style="width: 100px; height: 100px"
                            :src="'http://localhost:9999/img/head/'+scope.row.headimg"
                            fit="fill"></el-image>
                </template>
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
            >
                <template slot-scope="scope">
                    <el-button @click="showWin(scope.row)" type="text" size="small">编辑</el-button>
                    <el-button type="danger" @click="del(scope.row)" icon="el-icon-delete" circle></el-button>
                    <el-button @click="cz(scope.row)" type="text" size="small">重置密码</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                @current-change="currentChange"
                layout="prev, pager, next"
                :page-size="pageSize"
                :current-page="currentPage"
                :total="total">
        </el-pagination>
    </div>
</template>

<script>

    export default {
        created() {
            this.loadData();
            },
        name: "users",
        data(){
            return{
                tableData:[],
                imageUrl: "",
                upPass:true,
                uid:"",
                visible2: false,
                uName:"",
                pageSize:5,
                currentPage:1,
                total:1,
                dialogVisible:false,
                title:"",
                form:{
                    username:"root",
                    password:654321
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                    ]
                }
            }
        },
        methods:{
            loadData(){
                this.$http.post("users/selectAll",{UserName:this.uName,currentPage:this.currentPage,PageSize:this.pageSize}).then(res=>{
                    console.info(res)
                    this.tableData=res.data.list;
                    this.total=res.data.total;
                })
            },
            currentChange(currentPage){
                this.currentPage=currentPage;
                this.loadData()
            },
            load(){
                this.loadData()
            },
            showWin(row){
                this.dialogVisible=true;
                if (row){
                    this.form=Object.assign({},row);
                    this.upPass=true
                    this.imageUrl = "http://localhost:9999/img/head/"+row.headimg;
                    this.title="修改用户信息"
                }else {
                    this.form={}
                    this.upPass=false
                    this.imageUrl=""
                    this.title="新增用户信息 新创建的账户密码为123456"
                }
            },
            saveOrUpdate() {
                this.$http.post("users/saveOrUpdate",this.form).then(res=>{
                    if (res.data.success){
                        this.dialogVisible=false
                        this.loadData()
                    }
                })
            },
            del(row){
                this.$http.post("users/delete",{id:row.id}).then(res=>{
                    if (res.data.success){
                        this.loadData()
                    }
                })
            },
            cz(row){
                this.visible2=true;
                this.uid=row.id;
            },
            yes(){
                this.$http.post("users/czmm",{id:this.uid}).then(res=>{
                    if (res.data.success){
                        this.visible2=false;
                        this.loadData()
                    }
                })
            },
            handleAvatarSuccess(res) {
                console.info("aaa",res)
                this.imageUrl = "http://localhost:9999/img/head/"+res.target;
                this.form.headimg=res.target;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }

        }
    }
</script>

<style scoped>
.addOne{
    margin: 5px;
}
 .el-upload1 {
     width: 178px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
 .el-upload1:hover {
     width: 178px;
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>